package com.snow.useviewdemo.function.transition;

import android.os.Build;
import android.os.Bundle;
import android.transition.TransitionSet;
import android.view.Window;
import android.widget.ImageView;

import com.snow.img.ImageUtil;
import com.snow.useviewdemo.Constants;
import com.snow.useviewdemo.R;
import com.snow.useviewdemo.base.BaseActivity;

import butterknife.BindView;

/**
 * Created by zhaoyingtao
 * Date: 2021/6/24
 * Describe:共享元素 效果 Activity
 */
public class SharedElementsActivity extends BaseActivity {

    @BindView(R.id.imageView)
    ImageView imageView;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_shared_elements;
    }

    @Override
    protected void setViewData(Bundle savedInstanceState) {
        ImageUtil.imageLoad(this, Constants.TEST_IMAGE, imageView);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
//            getWindow().setEnterTransition(new CommentEnterTransition(this, imageView, imageView));
//            getWindow().postponeEnterTransition(new CommentEnterTransition(this, imageView, imageView));
            getWindow().setSharedElementEnterTransition(buildShareElemEnterSet());
            getWindow().setSharedElementEnterTransition(buildShareElemEnterSet());
            getWindow().setSharedElementReturnTransition(buildShareElemReturnSet());
        }
    }
//
//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            finishCurrentActivity();
//            return true;
//        }
//        return super.onKeyDown(keyCode, event);
//    }
//
//    private void finishCurrentActivity() {
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            ActivityCompat.finishAfterTransition(this);
//        } else {
//            finish();
//        }
//    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    /**
     * 分享 元素 进入动画
     * @return
     */
    private TransitionSet buildShareElemEnterSet() {
        TransitionSet transitionSet = new TransitionSet();
//
//        Transition changePos = new ChangePosition();
//        changePos.setDuration(300);
//        changePos.addTarget(R.id.comment_box);
//        transitionSet.addTransition(changePos);
//
//        Transition revealTransition = new ShareElemEnterRevealTransition(mCommentBox);
//        transitionSet.addTransition(revealTransition);
//        revealTransition.addTarget(R.id.comment_box);
//        revealTransition.setInterpolator(new FastOutSlowInInterpolator());
//        revealTransition.setDuration(300);

//        ChangeColor changeColor = new ChangeColor(getResources().getColor(R.color.black_85_alpha), getResources().getColor(R.color.white));
//        changeColor.addTarget(R.id.comment_box);
//        changeColor.setDuration(350);
//
//        transitionSet.addTransition(changeColor);

        transitionSet.setDuration(900);

        return transitionSet;
    }

    /**
     * 分享元素返回动画
     * @return
     */
    private TransitionSet buildShareElemReturnSet() {
        TransitionSet transitionSet = new TransitionSet();
//
//        Transition changePos = new ShareElemReturnChangePosition();
//        changePos.addTarget(R.id.comment_box);
//        transitionSet.addTransition(changePos);
//
//        ChangeColor changeColor = new ChangeColor(getResources().getColor(R.color.white), getResources().getColor(R.color.black_85_alpha));
//        changeColor.addTarget(R.id.comment_box);
//        transitionSet.addTransition(changeColor);
//        Transition revealTransition = new ShareElemReturnRevealTransition(mCommentBox);
//        revealTransition.addTarget(R.id.comment_box);
//        transitionSet.addTransition(revealTransition);
        transitionSet.setDuration(900);
        return transitionSet;
    }
}
